/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/

#pragma once

#include <memory>
#include <iostream>
#include <db/db_interfaces.h>

namespace test_wrappers { namespace storage {

template <class T>
class field_definition_t;

template <class T>
class field_value_t :
    public field_value
{
public:
    // instantiation
    field_value_t(
        const field_id& fieldInstanceId,
        T value) :
        _fieldInstanceId(fieldInstanceId),
        _value(value),
        _isSet(false)
    {
    }

    static std::shared_ptr<field_value_t<T>> create(
        const field_id& fieldInstanceId,
        T value)
    {
        return std::make_shared<field_value_t<T>>(fieldInstanceId, value);
    }

    static std::shared_ptr<field_value_t<T>> createByMoving(
        const field_id& fieldInstanceId,
        T& value)
    {
        throw std::logic_error("field_value_t::createByMoving() is not supported for this type.");
        return nullptr;
    }

    // getters
    field_id fieldInstanceId()
    {
        return _fieldInstanceId;
    }
    size_t size()
    {
        return sizeof(T);
    }
    void* buffer()
    {
        return &_value;
    }
    bool set()
    {
        return _isSet;
    }
    void valueByMoving(T& value)
    {
        throw std::logic_error("field_value_t::valueByMoving() is not supported for this type.");
    }
    T value()
    {
        return _value;
    }
    bool appendOnUpdate()
    {
        return false;
    }

    // setters
    void setValue(T value)
    {
        _value = value;
        _isSet = true;
    }
    void setValueByMoving(T& value)
    {
        throw std::logic_error("field_value_t::setValueByMoving() is not supported for this type.");
    }

    void setAppendOnUpdate(bool append)
    {
        throw std::logic_error("field_value_t::setAppendOnUpdate() is not supported for this type.");
    }
private:
    field_id _fieldInstanceId;
    bool _isSet;
    T _value;
};

template <>
class field_value_t <std::wstring> : public field_value
{
public:
    // instantiation
    field_value_t(
        const field_id& fieldInstanceId,
        std::wstring value) :
        _fieldInstanceId(fieldInstanceId),
        _value(value),
        _isSet(false)
    {
    }

    field_value_t(
        const field_id& fieldInstanceId) :
        _fieldInstanceId(fieldInstanceId),
        _value()
    {
    }
    static std::shared_ptr<field_value_t<std::wstring>> create(
        const field_id& fieldInstanceId,
        std::wstring value)
    {
        return std::make_shared<field_value_t<std::wstring>>(fieldInstanceId, value);
    }
    static std::shared_ptr<field_value_t<std::wstring>> createByMoving(
        const field_id& fieldInstanceId,
        std::wstring& value)
    {
        std::shared_ptr<field_value_t<std::wstring>> returnValue = std::make_shared<field_value_t<std::wstring>>(fieldInstanceId);
        returnValue->setValueByMoving(value);
        return returnValue;
    }

    // getters
    field_id fieldInstanceId()
    {
        return _fieldInstanceId;
    }
    size_t size()
    {
        return _value.size() * sizeof(wchar_t);
    }
    void* buffer()
    {
        return static_cast<void*>(const_cast<wchar_t*>(_value.c_str()));
    }

    bool set()
    {
        return _isSet;
    }
    void valueByMoving(std::wstring& value)
    {
        value = _value;
        _value = L"";
    }
    std::wstring value()
    {
        return _value;
    }

    bool appendOnUpdate()
    {
        return false;
    }

    // setters
    void setValue(std::wstring value)
    {
#if 0
        // We should re-enable this check here.
        std::shared_ptr<field_definition> fieldDefinition = std::dynamic_pointer_cast<field_definition>(_fieldDefinition.lock());
        if ((value.size() * sizeof(wchar_t)) > fieldDefinition->maxBufferSizeInBytes())
        {
            throw std::logic_error("Incoming string length greater than expected.  DB column size needs to be expanded.");
        }
#endif
        _value = value;
        _isSet = true;
    }
    void setValueByMoving(std::wstring& value)
    {
        _value = value;
        value = L"";
    }
    void setAppendOnUpdate(bool append)
    {
        throw std::logic_error("field_value_t::setAppendOnUpdate() is not supported for this type.");
    }

private:
    field_id _fieldInstanceId;
    std::wstring _value;
    bool _isSet;
};

template <>
class field_value_t <binary_blob> : public field_value
{
public:
    // instantiation
    field_value_t(
        const field_id& fieldInstanceId,
        binary_blob value) :
        _fieldInstanceId(fieldInstanceId),
        _value(value),
        _append(false),
        _isSet(false)
    {
    }

    field_value_t(
        const field_id& fieldInstanceId) :
        _fieldInstanceId(fieldInstanceId),
        _isSet(false)
    {
    }
    static std::shared_ptr<field_value_t<binary_blob>> create(
        const field_id& fieldInstanceId,
        binary_blob value)
    {
        return std::make_shared<field_value_t<binary_blob>>(fieldInstanceId, value);
    }
    static std::shared_ptr<field_value_t<binary_blob>> createByMoving(
        const field_id& fieldInstanceId,
        binary_blob& value)
    {
        std::shared_ptr<field_value_t<binary_blob>> returnValue = std::make_shared<field_value_t<binary_blob>>(fieldInstanceId);
        returnValue->setValueByMoving(value);
        return returnValue;
    }

    // getters
    field_id fieldInstanceId()
    {
        return _fieldInstanceId;
    }

    size_t size()
    {
        return _value._size;
    }

    void* buffer()
    {
        return _value._buffer;
    }
    bool set()
    {
        return _isSet;
    }
    void valueByMoving(binary_blob& value)
    {
        value.moveFrom(_value);
    }
    binary_blob value()
    {
        return _value;
    }

    bool appendOnUpdate()
    {
        return _append;
    }

    // setters
    void setValue(binary_blob value)
    {
#if 0
        // We should re-enable this check here.
        std::shared_ptr<field_definition> fieldDefinition = std::dynamic_pointer_cast<field_definition>(_fieldDefinition.lock());
        if (value._size > fieldDefinition->maxBufferSizeInBytes())
        {
            throw std::logic_error("Incoming blob size greater than expected.  DB column size needs to be expanded.");
        }
#endif
        _value.moveFrom(value);
        _isSet = true;
    }
    void setValueByMoving(binary_blob& value)
    {
        _value.moveFrom(value);
    }
    void setAppendOnUpdate(bool append)
    {
        _append = append;
    }

private:
    field_id _fieldInstanceId;
    binary_blob _value;
    bool _append;
    bool _isSet;
};

template <class T>
std::shared_ptr<field_value_t<T>> createValue(const field_id& fieldInstanceId, T value)
{
    return field_value_t<T>::create(fieldInstanceId, value);
}

template <class T>
std::shared_ptr<field_value_t<T>> createValueByMoving(const field_id& fieldInstanceId, T& value)
{
    return field_value_t<T>::createByMoving(fieldInstanceId, value);
}

}}// namespace test_wrappers::storage
